﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Engine.NetClasses
{
    public partial class ServerStateForm : Form
    {
        Timer tim;
        int countClient;
        public ServerState State
        {
            get;
            set;
        }
        public ServerStateForm()
        {
            InitializeComponent();
            Paint += new PaintEventHandler(ServerStateForm_Paint);
           // SetStyle(ControlStyles.UserPaint, true);
           // UpdateStyles();
            FormClosed += new FormClosedEventHandler(ServerStateForm_FormClosed);
            ClientList.DoubleClick += new EventHandler(ClientList_DoubleClick);
        }

       

        void ServerStateForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            tim.Stop();
            tim = null;    
        }

        void tim_Tick(object sender, EventArgs e)
        {
            State_Read();
        }

        void ServerStateForm_Paint(object sender, PaintEventArgs e)
        {
           //State_Read();
        }

        private void ServerStateForm_Load(object sender, EventArgs e)
        {
            MaxConnect.Text = State.MaxConnect.ToString();
            CurrentClients.Text = State.NumConnectedSockets.ToString();
            sessionNumber.Text = State.SesionCounter.ToString();
            if (State.Online)
            {
                StatServer.Text = "Online";
                StatServer.ForeColor = Color.Green;
            }
            else
            {
                StatServer.Text = "Offline";
                StatServer.ForeColor = Color.Red;
            }
            
            progressBar1.Maximum = State.MaxConnect;
            progressBar1.Minimum = 0;
            progressBar1.MarqueeAnimationSpeed = 100;           
            progressBar1.Value = State.NumConnectedSockets;
            progressBar1.Style = ProgressBarStyle.Continuous;
            State.PropertyChanged += new PropertyChangedEventHandler(State_PropertyChanged);
            countClient = State.Server.ConnectedClients.Count;
            ReadClients();
            tim = new Timer();
            tim.Interval=100;
            SetServerBooting();
            tim.Tick += new EventHandler(tim_Tick);
            tim.Start();
            
        }

        void State_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
           
        }

        void SetServerBooting()
        {
            if (progressBar1.Value<progressBar1.Maximum/2)
            {
                BootingServer.Text = "Low";
                BootingServer.ForeColor = Color.Green;
                progressBar1.ForeColor = Color.LightGreen;
            }
            if (progressBar1.Value>progressBar1.Maximum/2 && progressBar1.Value < progressBar1.Maximum / 2 +(progressBar1.Maximum / 4))
            {
                BootingServer.Text = "Average ";
                BootingServer.ForeColor = Color.Orange;
                progressBar1.ForeColor = Color.LightYellow;
            }
            if (progressBar1.Value > progressBar1.Maximum / 2 + (progressBar1.Maximum / 4) && progressBar1.Value < (progressBar1.Maximum - 10))
            {
                BootingServer.Text = "Higth";
                BootingServer.ForeColor = Color.Red;
                progressBar1.ForeColor =Color.LightCoral ;
            }
            if (progressBar1.Value > (progressBar1.Maximum - 10))
            {
                BootingServer.Text = "Very Higth";
                BootingServer.ForeColor = Color.Red;
                progressBar1.ForeColor = Color.Maroon;
            }
        }
        void State_Read()
        {
            
                    CurrentClients.Text = State.NumConnectedSockets.ToString();
                    progressBar1.Value = State.NumConnectedSockets;
                   
                    sessionNumber.Text = State.SesionCounter.ToString();                   
                    if (State.Online)
                    {
                        StatServer.Text = "Online";
                        StatServer.ForeColor = Color.Green;
                    }
                    else
                    {
                        StatServer.Text = "Offline";
                        StatServer.ForeColor = Color.Red;
                    }
                    SetServerBooting();
                    if (countClient!=State.Server.ConnectedClients.Count)
                    {
                         ReadClients();
                         countClient = State.Server.ConnectedClients.Count;
                    }
                   
             
        }

        private void ReadClients()
        {
            
            ClientList.Items.Clear();
            for (int i = 0; i < State.Server.ConnectedClients.Count; i++)
            {
                if ( !ClientList.Items.Contains("Client ID:"+State.Server.ConnectedClients[i].ID))
                {
                    ClientList.Items.Add("Client ID: " + State.Server.ConnectedClients[i].ID );
                }               
            }
           
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = ClientList.SelectedItem.ToString();
           // string text=ClientList.Items[ClientList.SelectedIndex].ToString();
            int selId =int.Parse(text.Replace("Client ID: ", ""));
        }
        void ClientList_DoubleClick(object sender, EventArgs e)
        {
            if (ClientList.SelectedIndex>=0)
            {
                
            
             string text=ClientList.Items[ClientList.SelectedIndex].ToString();
            int selId = int.Parse(text.Replace("Client ID: ", ""));
            LogView lv = new LogView();

            lv.Text = "log client id: " + selId;
            lv.textBox1.Text = State.Server.ServerLog.ReadLogs(selId);
            lv.textBox1.TextAlign = HorizontalAlignment.Left;
            lv.Show();
            }
        }
       
    }
}
